﻿camSchoolApp.controller("loginController", function ($http, $scope, $location, dataLayerService) {

    dataLayerService.currentLoggedUser = null;
    
    $scope.login = function () {
        showLoadingAnimation(130, 560, loadingAnimationTemplate + message);

        dataLayerService.loginAsynC().then(function (result) {
            dataLayerService.userViewModel = result;
            if (result === 0 || result === "") {
                //add error-class on TextBoxes
                $("#username").addClass("error-state");
                $("#password").addClass("error-state");
                displayErrorLogin("Erreur", "Vos identifiants sont incorrect.");
                return;
            }
            initTreeView();
            employeesDashBoardView();
        }, function (error) {            
            displayErrorLogin("Problem de Reseau", "Server n'est pas disponible pour l'instant.");
        });

        function initTreeView() {

            //load ClassTree data
            updateLoadingAnimation(130, 660, "Initialisation de la page d' acceuil.");

            dataLayerService.getClassRoomTypeAndChildrenAsynC().then(function (result) {
                dataLayerService.treeViewModel = result;
            }, function (error) {
                //go back to startpage
                $location.path("/startPage.html");
            });
        };

        function employeesDashBoardView() {

            //load ClassTree data
            updateLoadingAnimation(130, 660, "Initialisation de la page d' acceuil.");

            dataLayerService.getAllEmployeeAsynC().then(function (result) {
                dataLayerService.employeesViewModel = result;
                //go to mainpage
                $location.path("/mainPage");
                //Hide animation
                hideLoadingAnimation();
            }, function (error) {
                //go back to startpage
                $location.path("/startPage.html");
            });
        };

    }
});
